#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>

#if defined(__linux__)
// Linux系统
#include <unistd.h>
#elif defined(_WIN32)
// Windows系统
#include <windows.h>
#endif

/*单位：秒*/
void s_sleep_sec(int time)
{
#if defined(__linux__)
    // Linux系统
    sleep(time);
#elif defined(_WIN32)
    // Windows系统
    Sleep((time * 1000));
#endif
}

/*单位：毫秒*/
void s_sleep_millis(int time)
{
#if defined(__linux__)
    // Linux系统
    usleep(time * 1000);
#elif defined(_WIN32)
    // Windows系统
    Sleep(time);
#endif
}

int main()
{

    struct timeval start, end;
    long cost;

    gettimeofday(&start, NULL);
    printf("start sec=%u\n", start);
    printf("start microsec=%u\n", start.tv_usec);

    s_sleep_millis(500);

    gettimeofday(&end, NULL);
    printf("end sec=%u\n", end);
    printf("end microsec=%u\n", end.tv_usec);

    cost = end.tv_usec - start.tv_usec;
    printf("cost=%u\n", cost);

    return 0;
}
